const fs = require('fs')
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const tee = require('@babel/types');
const generator = require("@babel/generator").default;

function openTo(text) {
    function mainIn(text, jcVin, idKey) {
        function main(text, pageOfText) {
            var ast = parser.parse(text.toString(), {
                sourceType: "module"
            })
            var JSfeatures = {};

            // 结构树
            var listSt = [];

            var haveIn = [];

            var letbeRuue = -1;

            function cbb() {
                haveIn.push(0)
                if ('cbbFunctionDeclaration' in JSfeatures) {
                    JSfeatures['cbbFunctionDeclaration'] += 1
                } else {
                    JSfeatures['cbbFunctionDeclaration'] = 1
                }
                ;

                letbeRuue += 1;
                if (listSt[letbeRuue] == undefined) {
                    listSt.push(0);
                } else {
                    listSt[letbeRuue] += 1;

                }
            }

            function cbbl(path, lenAbot, findType) {

                if (haveIn.length == lenAbot) {
                    // Ident Numb Strig 兄弟节点 IntSum func结构
                    getBianlian = [0, 0, 0, 0, 0, ''];
                    var yhh = 0
                    path.traverse({
                        Identifier(path) {
                            getBianlian[0] += 1;
                            if (yhh == 0) {
                                yhh = 1
                                let body = path.find((p) => eval('p.is' + findType + '()')).node.body.body
                                if (body == undefined){
                                    return;
                                }
                                getBianlian[3] = body.length;
                                body.forEach(function (x) {
                                    getBianlian[5] += x.type[0]
                                })

                            }

                        },
                        NumericLiteral(path) {
                            getBianlian[1] += 1
                            // getBianlian[4] += path.node.value
                        },
                        StringLiteral(path) {
                            getBianlian[2] += 1
                        }

                    })
                    getKf.push(listSt.concat(getBianlian).join('-'))
                }
                ;
            }

            const getKf = [];


            const getAstFunc = (pathParent) => {

                pathParent.traverse({
                    FunctionExpression(path) {
                        cbb()
                        let lenAbot = haveIn.length
                        getAstFunc(path);
                        // console.log(listSt)
                        if (idKey == 1) {
                            let pol = 0
                            for (let i = 0; i < jcVin.every['1002'][0][0][0].length; i++) {
                                if (jcVin.every['1002'][1][0][0][i] == 0) {

                                    pol += 1
                                    continue
                                }

                                if (jcVin.every['1002'][0][0][0][i] == listSt[i]) {
                                    pol += 1
                                }
                            }
                            // console.log(pol)

                            if (pol == jcVin.every['1002'][0][0][0].length) {
                                var listIdof = ['', '', -1, ''];
                                path.node.params.forEach((x) => {
                                    listIdof[3] += x.type[0]
                                })

                                var cbbYhh = {
                                    NumericLiteral(pat) {
                                        listIdof[0] += pat.node.value;
                                    },
                                    StringLiteral(pat) {
                                        listIdof[1] += pat.node.value;
                                    },
                                    Identifier(pat) {

                                        listIdof[2] += 1;
                                    },
                                }
                                path.traverse(cbbYhh);


                                for (bnbnb in jcVin.every) {

                                    var funBianl = -1
                                    var wz = 0

                                    path.parentPath.traverse({
                                        Identifier(l) {
                                            wz += 1
                                            if (jcVin.every[bnbnb][3][0][1] == 1 && wz == jcVin.every[bnbnb][3][0][0]) {
                                                funBianl = l.node.name
                                            }
                                        }
                                    })

                                    let pol = 0;
                                    for (let i = 0; i < jcVin.every[bnbnb][0][0][1].length; i++) {
                                        if (jcVin.every[bnbnb][1][0][1][i] == 0) {
                                            pol += 1
                                            continue
                                        }
                                        if (i == 2) {
                                            if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i] + 1) {
                                                pol += 1
                                            } else if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i]) {
                                                pol += 1
                                            }
                                            continue
                                        }
                                        if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i]) {
                                            pol += 1
                                        }
                                    }
                                    ;

                                    if (pol == jcVin.every[bnbnb][1][0][1].length) {

                                        path.parentPath.traverse({
                                                BlockStatement(p) {
                                                    let pol2 = 0;

                                                    global.listIdof2 = ['', '', -1, ''];
                                                    let cbbYhh2 = {
                                                        NumericLiteral(pat) {
                                                            listIdof2[0] += pat.node.value;
                                                        },
                                                        StringLiteral(pat) {

                                                            listIdof2[1] += pat.node.value;
                                                        },
                                                        Identifier(pat) {
                                                            listIdof2[2] += 1;
                                                        },
                                                    }
                                                    p.traverse(cbbYhh2)
                                                    // console.log(listIdof2)
                                                    for (let i = 0; i < jcVin.every[bnbnb][1][1][1].length; i++) {
                                                        if (jcVin.every[bnbnb][1][1][1][i] == 0) {
                                                            pol2 += 1
                                                            continue
                                                        }
                                                        if (i == 2) {
                                                            if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i] + 1) {
                                                                pol2 += 1
                                                            } else if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i]) {
                                                                pol2 += 1
                                                            }
                                                            continue
                                                        }
                                                        if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i]) {
                                                            pol2 += 1
                                                        }
                                                    }
                                                    ;

                                                    if (pol2 == jcVin.every[bnbnb][1][1][1].length) {
                                                        if (jcVin.every[bnbnb][4][1][0] == 1) {
                                                            if (jcVin.every[bnbnb][4][1][2] == 1) {
                                                                // p.traverse({
                                                                // BlockStatement(p) {
                                                                if (p.node.body[jcVin.every[bnbnb][4][0][0] - 1] != undefined) {
                                                                    if (jcVin.every[bnbnb][4][0][2] == p.node.body[jcVin.every[bnbnb][4][0][0] - 1].type) {
                                                                        let wehave = p.get('body')[jcVin.every[bnbnb][4][0][0] - 1];
                                                                        let bui = -1;
                                                                        let jlop = 0;
                                                                        p.traverse({

                                                                            Identifier(l) {
                                                                                jlop += 1
                                                                                if (jcVin.every[bnbnb][3][1][1] == 1 && jlop == jcVin.every[bnbnb][3][1][0]) {
                                                                                    bui = l.node.name
                                                                                }
                                                                            }

                                                                        })
                                                                        if (bui != -1) {
                                                                            if (jcVin.every[bnbnb][2][jcVin.every[bnbnb][2].length - 1] == ';') {
                                                                                jcVin.every[bnbnb][2].slice(0, [jcVin.every[bnbnb][2].length - 2])
                                                                            }
                                                                            wehave.replaceInline(parser.parse(wehave + "" + jcVin.every[bnbnb][2].replace('{{inEval}}', bui)).program.body)
                                                                            p.stop()

                                                                        } else if (funBianl != -1) {
                                                                            wehave.replaceWithSourceString(wehave + "" + generator(jcVin.every[bnbnb][2]).code.replace('{{inEval}}', funBianl))
                                                                            p.stop()
                                                                        }

                                                                    }
                                                                }
                                                                ;

                                                                // }

                                                                // });
                                                                // p.stop();
                                                            }
                                                        }
                                                    }

                                                }
                                            }
                                        );
                                    }


                                }


                            }
                        }


                        cbbl(path, lenAbot, "FunctionExpression")
                        letbeRuue -= 1
                        path.skip()
                    },
                    ArrowFunctionExpression(path) {
                        cbb()
                        let lenAbot = haveIn.length
                        getAstFunc(path);
                        // console.log(listSt)
                        if (idKey == 1) {
                            let pol = 0
                            for (let i = 0; i < jcVin.every['1002'][0][0][0].length; i++) {
                                if (jcVin.every['1002'][1][0][0][i] == 0) {

                                    pol += 1
                                    continue
                                }

                                if (jcVin.every['1002'][0][0][0][i] == listSt[i]) {
                                    pol += 1
                                }
                            }
                            // console.log(pol)

                            if (pol == jcVin.every['1002'][0][0][0].length) {
                                var listIdof = ['', '', -1, ''];
                                path.node.params.forEach((x) => {
                                    listIdof[3] += x.type[0]
                                })

                                var cbbYhh = {
                                    NumericLiteral(pat) {
                                        listIdof[0] += pat.node.value;
                                    },
                                    StringLiteral(pat) {
                                        listIdof[1] += pat.node.value;
                                    },
                                    Identifier(pat) {

                                        listIdof[2] += 1;
                                    },
                                }
                                path.traverse(cbbYhh);


                                for (bnbnb in jcVin.every) {

                                    var funBianl = -1
                                    var wz = 0

                                    path.parentPath.traverse({
                                        Identifier(l) {
                                            wz += 1
                                            if (jcVin.every[bnbnb][3][0][1] == 1 && wz == jcVin.every[bnbnb][3][0][0]) {
                                                funBianl = l.node.name
                                            }
                                        }
                                    })

                                    let pol = 0;
                                    for (let i = 0; i < jcVin.every[bnbnb][0][0][1].length; i++) {
                                        if (jcVin.every[bnbnb][1][0][1][i] == 0) {
                                            pol += 1
                                            continue
                                        }
                                        if (i == 2) {
                                            if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i] + 1) {
                                                pol += 1
                                            } else if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i]) {
                                                pol += 1
                                            }
                                            continue
                                        }
                                        if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i]) {
                                            pol += 1
                                        }
                                    }
                                    ;

                                    if (pol == jcVin.every[bnbnb][1][0][1].length) {

                                        path.parentPath.traverse({
                                                BlockStatement(p) {
                                                    let pol2 = 0;

                                                    global.listIdof2 = ['', '', -1, ''];
                                                    let cbbYhh2 = {
                                                        NumericLiteral(pat) {
                                                            listIdof2[0] += pat.node.value;
                                                        },
                                                        StringLiteral(pat) {

                                                            listIdof2[1] += pat.node.value;
                                                        },
                                                        Identifier(pat) {
                                                            listIdof2[2] += 1;
                                                        },
                                                    }
                                                    p.traverse(cbbYhh2)
                                                    // console.log(listIdof2)
                                                    for (let i = 0; i < jcVin.every[bnbnb][1][1][1].length; i++) {
                                                        if (jcVin.every[bnbnb][1][1][1][i] == 0) {
                                                            pol2 += 1
                                                            continue
                                                        }
                                                        if (i == 2) {
                                                            if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i] + 1) {
                                                                pol2 += 1
                                                            } else if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i]) {
                                                                pol2 += 1
                                                            }
                                                            continue
                                                        }
                                                        if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i]) {
                                                            pol2 += 1
                                                        }
                                                    }
                                                    ;

                                                    if (pol2 == jcVin.every[bnbnb][1][1][1].length) {
                                                        if (jcVin.every[bnbnb][4][1][0] == 1) {
                                                            if (jcVin.every[bnbnb][4][1][2] == 1) {
                                                                // p.traverse({
                                                                // BlockStatement(p) {
                                                                if (p.node.body[jcVin.every[bnbnb][4][0][0] - 1] != undefined) {
                                                                    if (jcVin.every[bnbnb][4][0][2] == p.node.body[jcVin.every[bnbnb][4][0][0] - 1].type) {
                                                                        let wehave = p.get('body')[jcVin.every[bnbnb][4][0][0] - 1];
                                                                        let bui = -1;
                                                                        let jlop = 0;
                                                                        p.traverse({

                                                                            Identifier(l) {
                                                                                jlop += 1
                                                                                if (jcVin.every[bnbnb][3][1][1] == 1 && jlop == jcVin.every[bnbnb][3][1][0]) {
                                                                                    bui = l.node.name
                                                                                }
                                                                            }

                                                                        })
                                                                        if (bui != -1) {
                                                                            if (jcVin.every[bnbnb][2][jcVin.every[bnbnb][2].length - 1] == ';') {
                                                                                jcVin.every[bnbnb][2].slice(0, [jcVin.every[bnbnb][2].length - 2])
                                                                            }
                                                                            wehave.replaceInline(parser.parse(wehave + "" + jcVin.every[bnbnb][2].replace('{{inEval}}', bui)).program.body)
                                                                            p.stop()

                                                                        } else if (funBianl != -1) {
                                                                            wehave.replaceWithSourceString(wehave + "" + generator(jcVin.every[bnbnb][2]).code.replace('{{inEval}}', funBianl))
                                                                            p.stop()
                                                                        }

                                                                    }
                                                                }
                                                                ;

                                                                // }

                                                                // });
                                                                // p.stop();
                                                            }
                                                        }
                                                    }

                                                }
                                            }
                                        );
                                    }


                                }


                            }
                        }


                        cbbl(path, lenAbot, "ArrowFunctionExpression")
                        letbeRuue -= 1
                        path.skip()
                    }
                    ,
                    FunctionDeclaration(path) {
                        cbb()
                        let lenAbot = haveIn.length
                        getAstFunc(path);
                        // console.log(listSt)
                        if (idKey == 1) {
                            let pol = 0
                            for (let i = 0; i < jcVin.every['1002'][0][0][0].length; i++) {
                                if (jcVin.every['1002'][1][0][0][i] == 0) {

                                    pol += 1
                                    continue
                                }

                                if (jcVin.every['1002'][0][0][0][i] == listSt[i]) {
                                    pol += 1
                                }
                            }
                            // console.log(pol)

                            if (pol == jcVin.every['1002'][0][0][0].length) {
                                var listIdof = ['', '', -1, ''];
                                path.node.params.forEach((x) => {
                                    listIdof[3] += x.type[0]
                                })

                                var cbbYhh = {
                                    NumericLiteral(pat) {
                                        listIdof[0] += pat.node.value;
                                    },
                                    StringLiteral(pat) {
                                        listIdof[1] += pat.node.value;
                                    },
                                    Identifier(pat) {

                                        listIdof[2] += 1;
                                    },
                                }
                                path.traverse(cbbYhh);


                                for (bnbnb in jcVin.every) {

                                    var funBianl = -1
                                    var wz = 0

                                    path.parentPath.traverse({
                                        Identifier(l) {
                                            wz += 1
                                            if (jcVin.every[bnbnb][3][0][1] == 1 && wz == jcVin.every[bnbnb][3][0][0]) {
                                                funBianl = l.node.name
                                            }
                                        }
                                    })

                                    let pol = 0;
                                    for (let i = 0; i < jcVin.every[bnbnb][0][0][1].length; i++) {
                                        if (jcVin.every[bnbnb][1][0][1][i] == 0) {
                                            pol += 1
                                            continue
                                        }
                                        if (i == 2) {
                                            if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i] + 1) {
                                                pol += 1
                                            } else if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i]) {
                                                pol += 1
                                            }
                                            continue
                                        }
                                        if (jcVin.every[bnbnb][0][0][1][i] == listIdof[i]) {
                                            pol += 1
                                        }
                                    }
                                    ;

                                    if (pol == jcVin.every[bnbnb][1][0][1].length) {

                                        path.parentPath.traverse({
                                                BlockStatement(p) {
                                                    let pol2 = 0;

                                                    global.listIdof2 = ['', '', -1, ''];
                                                    let cbbYhh2 = {
                                                        NumericLiteral(pat) {
                                                            listIdof2[0] += pat.node.value;
                                                        },
                                                        StringLiteral(pat) {

                                                            listIdof2[1] += pat.node.value;
                                                        },
                                                        Identifier(pat) {
                                                            listIdof2[2] += 1;
                                                        },
                                                    }
                                                    p.traverse(cbbYhh2)
                                                    // console.log(listIdof2)
                                                    for (let i = 0; i < jcVin.every[bnbnb][1][1][1].length; i++) {
                                                        if (jcVin.every[bnbnb][1][1][1][i] == 0) {
                                                            pol2 += 1
                                                            continue
                                                        }
                                                        if (i == 2) {
                                                            if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i] + 1) {
                                                                pol2 += 1
                                                            } else if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i]) {
                                                                pol2 += 1
                                                            }
                                                            continue
                                                        }
                                                        if (jcVin.every[bnbnb][0][1][1][i] == listIdof2[i]) {
                                                            pol2 += 1
                                                        }
                                                    }
                                                    ;

                                                    if (pol2 == jcVin.every[bnbnb][1][1][1].length) {
                                                        if (jcVin.every[bnbnb][4][1][0] == 1) {
                                                            if (jcVin.every[bnbnb][4][1][2] == 1) {
                                                                // p.traverse({
                                                                // BlockStatement(p) {
                                                                if (p.node.body[jcVin.every[bnbnb][4][0][0] - 1] != undefined) {
                                                                    if (jcVin.every[bnbnb][4][0][2] == p.node.body[jcVin.every[bnbnb][4][0][0] - 1].type) {
                                                                        let wehave = p.get('body')[jcVin.every[bnbnb][4][0][0] - 1];
                                                                        let bui = -1;
                                                                        let jlop = 0;
                                                                        p.traverse({

                                                                            Identifier(l) {
                                                                                jlop += 1
                                                                                if (jcVin.every[bnbnb][3][1][1] == 1 && jlop == jcVin.every[bnbnb][3][1][0]) {
                                                                                    bui = l.node.name
                                                                                }
                                                                            }

                                                                        })
                                                                        if (bui != -1) {
                                                                            if (jcVin.every[bnbnb][2][jcVin.every[bnbnb][2].length - 1] == ';') {
                                                                                jcVin.every[bnbnb][2].slice(0, [jcVin.every[bnbnb][2].length - 2])
                                                                            }
                                                                            wehave.replaceInline(parser.parse(wehave + "" + jcVin.every[bnbnb][2].replace('{{inEval}}', bui)).program.body)
                                                                            p.stop()

                                                                        } else if (funBianl != -1) {
                                                                            wehave.replaceWithSourceString(wehave + "" + generator(jcVin.every[bnbnb][2]).code.replace('{{inEval}}', funBianl))
                                                                            p.stop()
                                                                        }

                                                                    }
                                                                }
                                                                ;

                                                                // }

                                                                // });
                                                                // p.stop();
                                                            }
                                                        }
                                                    }

                                                }
                                            }
                                        );
                                    }


                                }


                            }
                        }


                        cbbl(path, lenAbot, "FunctionDeclaration")
                        letbeRuue -= 1
                        path.skip()
                    }
                    ,
                })
            }

            const astGet = {
                FunctionExpression(path) {
                    cbb()
                    getAstFunc(path);
                    letbeRuue -= 1
                    path.skip()
                },
                ArrowFunctionExpression(path) {
                    cbb()
                    getAstFunc(path);
                    letbeRuue -= 1
                    path.skip()
                },
                FunctionDeclaration(path) {

                    // if (path.node.id.name == "vs") {
                    //     debugger;
                    // }
                    cbb()
                    getAstFunc(path);
                    letbeRuue -= 1
                    path.skip()
                },
            };


            traverse(ast, astGet);
            text2 = generator(ast,{
                compact:true
            }).code
            // console.log(listSt)
            return {'JSfeatures': JSfeatures, 'listSt': listSt, 'getKf': getKf}
        }


        let dataDist = main(text);
        if (idKey == 0) {
            let tzList = JSON.parse(JSON.stringify(jcVin.andYhh[0]));
            tzList.push(jcVin.firstCbb[0]);
            let yhhLo = jcVin.andYhh[1];
            yhhLo.push(jcVin.firstCbb[1]);
            for (let iin = 0; iin < tzList.length; iin++) {
                if (yhhLo[iin] == 1) {
                    if (iin == yhhLo.length - 1) {
                        if (dataDist.JSfeatures.cbbFunctionDeclaration != tzList[iin]) {
                            return text
                        }
                        ;
                    } else {
                        if (dataDist.listSt[iin] != tzList[iin]) {
                            return text
                        }
                        ;
                    }
                    ;
                }
                ;

            }
            ;
            for (let iin = 0; iin < jcVin.lastZcj.length; iin++) {
                if (dataDist.getKf.indexOf(jcVin.lastZcj[iin]) == -1) {
                    return text
                }
                ;
            }
            ;
            return mainIn(text, jcVin, 1);
        } else if (idKey == 1) {
            if (text2 == undefined) {
                return text
            }
            return text2
        }


    }


    var teZhen = fs.readFileSync('./jstz.txt', {encoding: "utf8"})
    return mainIn(text, JSON.parse(teZhen), 0)

}

module.exports = openTo;

